/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.yonsei.iwb.sdt.guix;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.ScrollPaneConstants;

/**
 *
 * @author DK
 */
public class OntologyURIFindViewFrame extends JPanel implements ActionListener{
//    private MainFrame mf;
//    private Controller ctrl;
    private LearningInfoViewPanel livp;    
//    private JLabel sourceLabel;
    private JList uriList;
    private DefaultListModel uriListModel;
    private JButton setButton;
    private JFrame frame;
    private Vector<String> rset;
    private JLabel label;
    private String resultURI;
    private int type;
    
    private static final int CLASSTYPELIST = 0;
    private static final int PROPERTYTYPELIST = 1;
    
    
    public OntologyURIFindViewFrame(Vector<String> rset, LearningInfoViewPanel livp, int listType){

        this.livp = livp;
        this.frame = new JFrame();        
        this.frame = new JFrame("Select URI from List");
        this.frame.setSize(700, 300);    
//        this.label = new JLabel();
//        this.label.setText("URI List");
//        this.add(this.label);
        this.uriListModel = new DefaultListModel();
        this.uriList = new JList(uriListModel);
        this.uriList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        this.setButton = new JButton("Select URI");
        this.setButton.addActionListener(this);
        this.rset = rset;
        this.setBorder(BorderFactory.createTitledBorder("Select URI List"));
        this.addURISetToList(rset);
        this.type = listType;
        this.add(this.uriList);
        
        
        JScrollPane tscroll = new JScrollPane(this, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        this.add(this.setButton);
        
         this.uriList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e){                
                if(e.getClickCount() >= 2){
                    Object select = uriList.getSelectedValue().toString();
                    String s    = select.toString();
                    enterClickedResource(s);
                }
            }            
        });
        
        this.frame.add(tscroll);
    }
    
    public void showFrame(){
        this.frame.setPreferredSize(getMaximumSize());
        this.frame.setVisible(true);
    }
    
    private void enterClickedResource(String result){
         if(this.type == OntologyURIFindViewFrame.CLASSTYPELIST){
            this.livp.setTargetClassURI(result);
        }else if(this.type == OntologyURIFindViewFrame.PROPERTYTYPELIST){
            this.livp.setTargetPropertyURI(result);
        }
        this.closeFrame();
    }
    
    /**
     * return set of resources(for concept learning)
     * @return 
     */
    private List getSelectedResources(){
        if(!this.uriList.isSelectionEmpty()){
            List selectList = this.uriList.getSelectedValuesList();
            
            return selectList;
        }else{
            return null;
        }
        
    }
    
    private String getSelectedResource(){
//        String s = new String();
        
        if(!this.uriList.isSelectionEmpty()){
            Object select = this.uriList.getSelectedValue().toString();
            String s = select.toString();
            return s;
        }
        
        return null;
    }
    
        
    private void addURISetToList(Vector<String> rset){
        for (int i = 0; i < rset.size(); i++) {
            this.uriListModel.addElement(rset.get(i));            
        }        
    }

    public void closeFrame(){
        this.frame.dispose();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        Object o = e.getSource();
        if(o==this.setButton){
            
            String result = this.getSelectedResource();
            if(result != null){
                if(this.type == OntologyURIFindViewFrame.CLASSTYPELIST){
                    this.livp.setTargetClassURI(result);
                }else if(this.type == OntologyURIFindViewFrame.PROPERTYTYPELIST){
                    this.livp.setTargetPropertyURI(result);
                }
                this.closeFrame();
            }
            
            
            //*********************************** for concept learning
//            List selectList = this.getSelectedResources();            
//            if(selectList != null){
//                if(this.type == OntologyURIFindViewFrame.CLASSTYPELIST){
//                    this.livp.setInstanceSet(selectList);
//                }
//                this.closeFrame();
//            }
            //****************************************
        }
    }
    
}

